<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangowner;
use App\Models\Notice;
use App\Models\Renting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NoticeController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Notice $notice)
    {
        $data = $notice->with(["owner","renting"])->paginate($this->pagsize);
        return view("admin.notice.index",compact("notice","data"));
    }

    # 显示添加页面
    public function create()
    {

        $fData = json_encode(Fangowner::get()->toArray(),JSON_UNESCAPED_UNICODE);
        $rData = json_encode(Renting::get()->toArray(),JSON_UNESCAPED_UNICODE);
        return view("admin.notice.create",compact("fData","rData"));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        dump($request);
        $data = $request->except("_token");
        Notice::create($data);

        $message = ['status'=>200,'msg'=>'修改成功','url'=> route('admin.notice.index')];
        return json_encode($message);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function show(Notice $notice)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function edit(Notice $notice)
    {
        $fData = json_encode(Fangowner::get()->toArray(),JSON_UNESCAPED_UNICODE);
        $rData = json_encode(Renting::get()->toArray(),JSON_UNESCAPED_UNICODE);
        return view("admin.notice.edit",compact("fData","rData","notice"));
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Notice $notice)
    {
        $data = $request->except(["_token"]);
        $notice->update($data);
        return ["status" => 200,"msg" => "修改成功","url" => route("admin.notice.index")];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Notice  $notice
     * @return \Illuminate\Http\Response
     */
    public function destroy(Notice $notice)
    {
        //
    }
}
